package com.mud.mudcommune.view.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.mud.mudcommune.R;
import com.mud.mudcommune.view.widget.SwitchButton;
import com.mud.mudcommune.entry.SignUpEntry;
import com.mud.mudcommune.http.ApiService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Optional;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class  ApplyFragment extends Fragment implements Callback<SignUpEntry> {
    private static ApplyFragment fragment = new ApplyFragment();
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.case_back)
    ImageView back;
    @BindView(R.id.input_name)
    EditText name;
    @BindView(R.id.switch_button)
    SwitchButton sex;
    @BindView(R.id.wechat)
    EditText wechat;
    @BindView(R.id.tel)
    EditText tel;
    @BindView(R.id.address)
    EditText address;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;
    private int currentStatus = -1;
    private Unbinder unbinder;

    public static Fragment getInstance() {
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_apply, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        title.setText(R.string.rbApply);
    }

    @Optional
    @OnClick({R.id.submit, R.id.case_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.case_back:
                getFragmentManager().popBackStack();
                break;
            case R.id.submit:
                String mname = name.getText().toString();
                String msex = null;
                currentStatus = sex.getCurrentStatus();// 0是女 1是男
                if (currentStatus == 0) {
                    msex = "女";
                } else if (currentStatus == 1) {
                    msex = "男";
                }
                String mtel = tel.getText().toString();
                String mwechat = wechat.getText().toString();
                String maddress = address.getText().toString();
                if ("".equals(mname) || "".equals(mtel) || "".equals(mwechat) || "".equals(maddress)) {
                    Toast.makeText(getContext(), "所填项不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);
                    Retrofit retrofit = new Retrofit.Builder().baseUrl(ApiService.BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    ApiService apiService = retrofit.create(ApiService.class);
                    Call<SignUpEntry> call = apiService.
                            Sign_Up(mname, msex, mtel, mwechat, maddress);
                    call.enqueue(this);
                }
                break;
        }
    }

    @Override
    public void onResponse(Call<SignUpEntry> call, Response<SignUpEntry> response) {
        SignUpEntry body = response.body();
        if (body.getStatus() == 200) {
            mProgressBar.setVisibility(View.GONE);//显示信息就把进度条Gone掉
            Toast.makeText(getContext(), "报名成功", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(Call<SignUpEntry> call, Throwable t) {
        Toast.makeText(getContext(), "数据返回失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}


